#!/bin/bash
srcquota="/${QUOTA_TEST}/srcQuotaDir"
srcdir="${FS_MOUNTPOINT}${srcquota}"
destquota="/${QUOTA_TEST}/destQuotaDir"
destdir="${FS_MOUNTPOINT}${destquota}"
noquotadir="${FS_MOUNTPOINT}/${QUOTA_TEST}/dir_noquota"
LOGFILE="/tmp/quotalog"
global_used=0

msg0="测试所有用例";
msg1="同一个配额目录, 测试目标文件不存在";
msg2="同一个配额目录, 测试目标文件存在, src > desc (fileszie)";
msg3="同一个配额目录, 测试目标文件存在, src < desc (fileszie)";
maxmsg=3

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh test_one.sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function reset_all() {
    hard_limit=$(($1 * 1024 * 1024)) # 10 MB

    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    mkdir -p ${srcdir}; rm -rf ${srcdir}/*;

    orcafs-ctl --setquota --dir=${srcdir} --hardlimit=${hard_limit} &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====清空目录并重新创建配额目录 ${srcquota} 硬阈值为 $1 MB====="
    quota_num=`orcafs-ctl --listquota | grep -w DIR | grep ${srcquota} | wc -l`
    if ((${quota_num} == 1));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function rename_to_unexist_file() {
    rm -rf $srcdir/*
    dest_dir=$srcdir/childdir; mkdir -p $srcdir/childdir
    src_file=$srcdir/src_file; dest_file=$dest_dir/dest_file
    
    cmd="echo 12345 > $src_file"; echo -e "\t${cmd}"; eval ${cmd}
    old_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`

    cmd="mv $src_file $dest_file"; echo -e "\t${cmd}"; eval ${cmd}
    dest_file_size=`stat -c "%s" ${dest_file}`; echo -e "\tafter mv size: ${dest_file} = ${dest_file_size}";

    new_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    want=$old_used
    echo -e "\t测试 mv, 初始用量为 $old_used, 期望用量为 $want, 实际为 $new_used"
    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if (($new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi; echo ""
}

function rename_to_exist_file() {
    bs_size1=$(($1 * 1024 * 1024))
    bs_size2=$(($2 * 1024 * 1024))
    rm -rf $srcdir/*
    dest_dir=$srcdir/childdir; mkdir -p $srcdir/childdir
    src_file=$srcdir/src_file; dest_file=$dest_dir/dest_file
    
    cmd="dd if=/dev/zero of=${src_file} bs=${bs_size1} count=1"; echo -e "\t${cmd}"; eval ${cmd} &> /dev/null
    src_file_size=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    
    cmd="dd if=/dev/zero of=${dest_file} bs=${bs_size2} count=1"; echo -e "\t${cmd}"; eval ${cmd} &> /dev/null
    old_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`

    cmd="mv $src_file $dest_file"; echo -e "\t${cmd}"; eval ${cmd}
    #cmd="ls -l $dest_dir | grep -v total"; echo -n -e "\t${cmd}\t>>\t"; eval ${cmd}
    dest_file_size=`stat -c "%s" ${dest_file}`; echo -e "\tafter mv size: ${dest_file} = ${dest_file_size}";

    new_used=`orcafs-ctl --listquota | grep -E -w "${srcquota}" | awk '{print $8}'`
    want=$src_file_size
    
    echo -e "\t测试 mv, 初始用量为 $old_used, 期望用量为 $want, 实际为 $new_used"
    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if (($new_used != $want)); then echo "测试失败"; exit 1; else echo "测试成功"; fi; echo ""
}

echo "/*****************************************************"
echo "测试 rename/mv 文件====源文件和目标文件在同一个配额目录中"
echo "*****************************************************/"

if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
source ${QUOTA_COMMON_FUNC}
reset_all 10; # 10 MB

test_num=1; if (($1 == ${test_num})) || (($1 == 0));then rename_to_unexist_file; fi #1 MB
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then rename_to_exist_file 3 2; fi #12 MB
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then rename_to_exist_file 6 8; fi #1 MB

echo "/*************************************************************************"
echo "   测试 rename/mv 文件====源文件和目标文件在同一个配额目录中  all success    "
echo "*************************************************************************/"
exit 0;



